package com.huika.dealontop.control.shops.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.control.base.BaseFra;
import com.huika.dealontop.control.shops.holder.ProductEvaluationItemHolder;
import com.huika.dealontop.control.timelimit.activity.MemberEvaluateActicvity;
import com.huika.dealontop.support.domain.ProductDetailsBean;
import com.huika.dealontop.support.domain.ProductEvaluationBean;
import com.huika.dealontop.support.domain.ProductEvaluationItemBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.ToastMsg;

import java.util.List;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/23 13:20 <br/>
 * Description: 商品评价
 */
public class ProductEvaluationListFragment extends BaseFra implements Response.Listener<RequestResult<ProductEvaluationBean>>, AdapterView.OnItemClickListener {
    private ProductDetailsBean.ProductBean productDetailsBean;

    private boolean isFirstLoadData;

    private JSONWrapAjaxParams ajaxParams;

    private PullToRefreshListView mLvContent;
    private EvaluationListAdapter mAdapter;
    private View emptyView;
    private  int pageSize = 10;

    public PullToRefreshListView getPullToRefreshListView() {
        return mLvContent;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return View.inflate(UIUtils.getContext(), R.layout.fragment_evaluation_list, null);
    }

    @Override
    protected void initWidget(View parentView) {
        mLvContent = (PullToRefreshListView) parentView.findViewById(R.id.lv_evaluation);
        mLvContent.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        emptyView = parentView.findViewById(R.id.common_empty);
        ((TextView)emptyView.findViewById(R.id.load_empty)).setText(UIUtils.getString(R.string.no_product_evaluation));
        emptyView.findViewById(R.id.click_reload).setOnClickListener(this);
        mAdapter = new EvaluationListAdapter(this.getActivity());
        mLvContent.setAdapter(mAdapter);
        mLvContent.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (productDetailsBean != null) {
                    loadMoreData();
                }
            }
        });
        mLvContent.setOnItemClickListener(this);
    }

    public void firstLoadData(){
        if(!isFirstLoadData && productDetailsBean != null){
            startLoadData();
        }
    }

    private void startLoadData() {
        JSONWrapAjaxParams ajaxParams = getParams();
        ajaxParams.putCommonTypeParam("pageNo", 1);
        FormResultRequest<ProductEvaluationItemBean> evaluationRequest = new FormResultRequest(UrlConstants.HOST + "Product/UserEvaluation",
                this, this,
                new TypeToken<RequestResult<ProductEvaluationBean>>(){}.getType());
        evaluationRequest.setRequestParams(ajaxParams);
        showLoadingDialog("");
        executeRequest(evaluationRequest);
    }



    private void loadMoreData() {
        JSONWrapAjaxParams ajaxParams = getParams();
        ajaxParams.putCommonTypeParam("pageNo", getCurrPage());
        FormResultRequest<ProductEvaluationItemBean> otherGrouponRequest = new FormResultRequest(UrlConstants.HOST + "Product/UserEvaluation",
                new Response.Listener<RequestResult<ProductEvaluationBean>>() {
                    @Override
                    public void onResponse(RequestResult<ProductEvaluationBean> requestResult) {
                        mLvContent.onRefreshComplete();
                        if(requestResult.flag == 1 && requestResult.getRs() != null){
                            List<ProductEvaluationItemBean> list = requestResult.getRs().getData();
                            mAdapter.addItems(list);
                            mLvContent.setMode(list != null && list.size() >= 10 ? PullToRefreshBase.Mode.PULL_FROM_END : PullToRefreshBase.Mode.DISABLED);
                        }else{
                            ToastMsg.showToastMsg(UIUtils.getContext(), "加载更多失败");
                        }
                    }
                }, this,
                new TypeToken<RequestResult<ProductEvaluationBean>>(){}.getType());
        otherGrouponRequest.setRequestParams(ajaxParams);
        executeRequest(otherGrouponRequest);
    }

    @Override
    protected void widgetClick(View v) {
        if(v.getId() == R.id.click_reload){
            startLoadData();
        }
        super.widgetClick(v);
    }

    @NonNull
    private JSONWrapAjaxParams getParams() {
        if(ajaxParams == null){
            ajaxParams = new JSONWrapAjaxParams();
            ajaxParams.putCommonTypeParam("productID", productDetailsBean.getProductId());
//            evaluationListParams.putCommonTypeParam("productID", "FWaSp7MkdmB1A0/hcdWhisldsaZuBlRiCemVyoZnn+/QldnJBAJCPTrNkbv38slfFd/i35LtdkbCxdiZn4SqyrEdkm4CDPAh00I65Dm2dACQ9PO8fRnOCa3k0KaEgvAFaJf0TUDJbhQnxETR0GMCCHzhLTofjy0721GvzJm5ikM=");
            ajaxParams.putCommonTypeParam("MerchantID", productDetailsBean.getMerchantID());
//            evaluationListParams.putCommonTypeParam("MerchantID", "");
            ajaxParams.putCommonTypeParam("type", 1);
            ajaxParams.putCommonTypeParam("pageSize", pageSize);
        }
        return ajaxParams;
    }

    public void setProductDetails(ProductDetailsBean.ProductBean productDetails){
        this.productDetailsBean = productDetails;
    }

    @Override
    public void onResponse(RequestResult<ProductEvaluationBean> requestResult) {
        dismissLoadingDialog();
        isFirstLoadData = true;
        mAdapter.clearGroup(true);
        if(requestResult.flag == 1 && requestResult.getRs() != null) {
            mAdapter.setGroup(requestResult.getRs().getData());
        }
        mLvContent.setVisibility(mAdapter.getCount() > 0 ? View.VISIBLE : View.GONE);
        emptyView.setVisibility(mAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
        mLvContent.setMode(mAdapter.getCount() >= 10 ? PullToRefreshBase.Mode.PULL_FROM_END : PullToRefreshBase.Mode.DISABLED);
    }

    public int getCurrPage() {
        return (mAdapter.getCount() + (10 - 1))  / 10;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ProductEvaluationItemBean evaluationItem = mAdapter.getItem(i - 1);
        if(evaluationItem == null) return;
        Intent intent = new Intent(getActivity(), MemberEvaluateActicvity.class);
        intent.putExtra(Constants.EVALUATE_ID, evaluationItem.getEvaluateID());
        startActivity(intent);
    }

    class EvaluationListAdapter extends BaseAda<ProductEvaluationItemBean> {
        public EvaluationListAdapter(Context context) {
            super(context);
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ProductEvaluationItemHolder holder;
            if(view == null){
                holder = new ProductEvaluationItemHolder(getActivity());
                view = holder.getRootView();
            }else {
                holder = (ProductEvaluationItemHolder) view.getTag();
            }
            holder.setData(getItem(i));
            return view;
        }
    }
}
